1490B - Balanced Remainders - CodeForces Solution


brute force constructive algorithms math *1000

Please click on ads to support us..

Python Code:

t = int(input())
for x in range(t) : 
    n=int(input())
    a=list(map(int,input().split()))
    c0,c1,c2=0,0,0
    for i in a : 
        if i%3==0 : 
            c0+=1
        elif i%3==1: 
            c1+=1
        else :
            c2+=1
    res=0
    have=n//3
        
    while (c0 != c1) or (c0 != c2) or (c2 != c1) :
            
        if c0>have : 
            res+=c0-have
            c1+=c0-have
            c0-=c0-have
        elif c1>have:
            res+=c1-have
            c2+=c1-have
            c1-=c1-have
        elif c2>have : 
            res+=c2-have
            c0+=c2-have
            c2-=c2-have
    print(res)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
void HOFNY() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define ll long long
#define sz(v) (int)v.size()
#define all(x) x.begin(), x.end()
#define h "\n"
#define m(frq,x) memset(frq,x,sizeof(frq))
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
int main() {
    HOFNY();
    int tc = 1; cin >> tc;
    while (tc--) {
        int n,cont=0; cin >> n;
        vector<int>v(n);
        int c1=0, c2=0, c0=0;
        for (int i = 0; i < n; i++) {
            cin >> v[i];
            if (v[i] % 3 == 0)c0++;
            else if (v[i]%3==1)c1++;
            else c2++;
        }
        int mx = max({ c0,c1,c2 });
        int mn = min({ c0,c1,c2 });
        while (mx > mn) {
            int mx = max({ c0,c1,c2 });
            int mn = min({ c0,c1,c2 });
            if (mx == mn)break;
            if (mx == c0) {
                if (mn == c1) {
                    c0--;
                    c1++;
                    cont++;
                }
                else {
                    c0--;
                    c2++;
                    cont+=2;
                }
            }
            else if (mx == c1) {
                if (mn == c0) {
                    c1--;
                    c0++;
                    cont+=2;
                }
                else {
                    c1--;
                    c2++;
                    cont ++;
                }
            }
            else if (mx == c2) {
                if (mn == c0) {
                    c2--;
                    c0++;
                    cont++;
                }
                else {
                    c2--;
                    c1++;
                    cont += 2;
                }
            }
        }
        cout << cont<<h;
    }
}


Comments

Submit
0 Comments
More Questions

1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111